package com.hong.atomic;

/**
 * @Auther: Administrator
 * @Date: 2018/7/19 15:11
 * @Description:
 */
public class AtomicIntegerDetailsTest2 {

    private final static CompareAndSetLock lock = new CompareAndSetLock();

    public static void main(String[] args) {
        for (int i =0;i<5;i++){
            new Thread(){
                @Override
                public void run() {
                    try {
                        doSomething2();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (GetLockException e) {
                        e.printStackTrace();
                    }
                }
            }.start();
        }

    }

    private static void doSomething() throws InterruptedException {
        synchronized (AtomicIntegerDetailsTest2.class){
            System.out.println(Thread.currentThread().getName() + "get the lock");
            Thread.sleep(1000000);
        }
    }

    private static void doSomething2() throws InterruptedException, GetLockException {
       try {
           lock.tryLock();
           System.out.println(Thread.currentThread().getName() + "get the lock");
           Thread.sleep(1000000);
       }finally {
            lock.unlock();
       }
    }
}
